using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Minions.Classes;
using Nsb.Classes;
using jQueryApi;

namespace MinionsEdit.Classes
{
    [IgnoreNamespace]
    public class MiPropPanel : PanelPopup
    {
        #region Css

        private const string PanelCss = @"
.MiEditPanel {
    width: 360px;
    min-height: 180px;
    background-color: rgba(0,0,0,.85);
    border: 1px solid white;
    font-family: calibri,arial;
    font-size: 10pt;
    line-height: 100%;
    -moz-border-radius: 8px;
    -webkit-border-radius: 8px;
    border-radius: 8px;
}
.MiEPLeft {
    position: absolute;
    top: 5px;
    left: 8px;
    width: 80px;
    bottom: 5px;
    background-color: rgba(255,255,255,.2);
} 
.MiEPRight {
    position: relative;
    top: 5px;
    left: 93px;
    right: 8px;
    xbottom: 5px;
    padding: 3px 5px;
    color: white;
    background-color: transparent;
    font-family: calibri,arial;
    font-size: 10pt;
}
.MiEPTabs {
    list-style-type: none;
    padding: 1px 3px;
}   
.MiEPTabs li {
    color: white;
    text-align: center;
    -moz-border-radius-topleft: 3px:
    -webkit-border-top-left-radius: 3px;
    border-top-left-radius: 3px;
    -moz-border-radius-bottomleft: 3px:
    -webkit-border-bottom-left-radius: 3px;
    border-bottom-left-radius: 3px;
}
.MiEPTabs li:hover {
    background-color: rgba(200,200,100,.5);
    cursor: default;
}
.MiEPTabOn {
    color: #FFA;
    background-color: rgba(173,216,230,.5);
}
        ";

        #endregion Css

        private static int _currentTabIdx;
        private static MiPropPanel _instance;
        private readonly jQueryObject _content;
        private readonly Dictionary<string, jQueryObject> _tabPanels = new Dictionary();
        private readonly jQueryObject _tabs;
        private readonly jQueryObject _tabsBox;
        public bool QueSave;
        private MinionEdit _mm;
        private int _tabIdx;

        static MiPropPanel()
        {
            AddStyle(PanelCss);
            AddStyle(MiColorPicker.PickerCss);
            //new Await()
            //    .LoadText("ColorPickerTable", Uri.Scripts("Data/GigColorPicker.html"))
            //    .Commit();
            //;
        }

        public MiPropPanel()
        {
            Element
                .AddClass("MiEditPanel")
                ;

            _tabsBox = jQuery.FromHtml("<div class='MiEPLeft'/>")
                .AppendTo(Element)
                ;

            _content = jQuery.FromHtml("<div class='MiEPRight'/>")
                .AppendTo(Element)
                ;

            _tabs = jQuery.FromHtml("<ul class='MiEPTabs'/>")
                .AppendTo(_tabsBox)
                ;

            Refocus();
            Tab("colors", "object colors").Click(ColorsTabEv);
            Tab("border", "object background colors").Click(BorderTabEv);
            Tab("font", "font type and size").Click(FontTabEv);
            Tab("layer", "layer properties").Click(LayerTabEv);
            if (!_mm.Data.relative)
                Tab("relative", "make size relative to containing object").Click(DestroyEv).Click(MinionEdit.ActiveMakeRelativeEv);
            Tab("remove", "remove the selected object").Click(DestroyEv).Click(MinionEdit.ActiveDeleteMinionEv);
        }

        public string Units
        {
            get { return MinionEdit.ActiveFocus.Units; }
        }

        private jQueryObject Tab(string text, string title)
        {
            int ti = _tabIdx++;
            jQueryObject tab = jQuery.FromHtml("<li/>");
            tab
                .AppendTo(_tabs)
                .Html(text)
                .Attribute("title", title)
                .Click(delegate
                       {
                           jQuery.Select(".MiEPTabOn").RemoveClass("MiEPTabOn");
                           tab.AddClass("MiEPTabOn");
                           _currentTabIdx = ti;
                       })
                ;
            return tab;
        }

        public static void Ev(jQueryEvent e)
        {
            if (e.ShiftKey)
                return;
            CancelEvent(e);
            MinionEdit.MaskSleeping = true;
            if (_instance == null)
                _instance = new MiPropPanel();
            _instance.ShowEv(e);
        }

        public void ShowEv(jQueryEvent e)
        {
            Refocus();
            if (_mm != null)
            {
                Pos p = (Pos) (object) _mm.Element.GetOffset();

                Element
                    .CSS(
                        new Dictionary(
                            "top", p.Top,
                            "left", p.Left + _mm.Element.GetOuterWidth()
                            ))
                    .Show()
                    .Find("li").Eq(_currentTabIdx).Trigger("click")
                    ;
            }
            else
            {
                DestroyEv(e);
            }
        }

        protected override void DestroyEv(jQueryEvent e)
        {
            _instance = null;
            MinionEdit.MaskSleeping = false;
            base.DestroyEv(e);
            if (QueSave)
                new Evo().FireWith(MinionEdit.ActiveFocus.Save);
        }

        private void Refocus()
        {
            if (MinionEdit.ActiveFocus != null)
            {
                _mm = MinionEdit.ActiveFocus;
            }
            else
            {
                _mm = null;
            }
        }

        #region Tabs

        private void ColorsTabEv(jQueryEvent e)
        {
            MiPropPage.Ev(this, _content)
                .Append(new MiColorWidget("Text", "color"))
                .NewBox()
                .Append(new MiColorWidget("Background", "background-color"))
                .Ender()
                ;
        }

        private void BorderTabEv(jQueryEvent e)
        {
            MiPropPage.Ev(this, _content)
                .Append(new MiColorWidget("Border", "border-color"))
                .NewBox()
                .Label("width")
                .Append(new MiBorderWidthWidget("BorderWidth", "border-width", 0, 9))
                .Label("")
                //.Append(new MiSelectWidget("Opacity", "opacity", MiProperty.AlphaValues))
                .Append(new MiBorderStyleWidget("style", "border-style",
                                                new Dictionary(
                                                    "none", "none",
                                                    "solid", "solid",
                                                    "dotted", "dotted",
                                                    "dashed", "dashed",
                                                    "inset", "inset",
                                                    "outset", "outset",
                                                    "groove", "groove",
                                                    "ridge", "ridge",
                                                    "double", "double"
                                                    ))
                )
                .Append(new MiBorderRadius("rounding", MiProperty.DictOf(0, 50)))
                .Label("padding")
                .Append(new MiPxWidget("top", "padding-top", MiProperty.DictOf(0, 50)))
                .Append(new MiPxWidget("left", "padding-left", MiProperty.DictOf(0, 50)))
                .Append(new MiPxWidget("right", "padding-right", MiProperty.DictOf(0, 50)))
                .Append(new MiPxWidget("bottom", "padding-bottom", MiProperty.DictOf(0, 50)))
                .Ender();
        }

        private void FontTabEv(jQueryEvent e)
        {
            MiPropPage.Ev(this, _content)
                .Append(new MiFontSizeWidget("FontSize", "font-size", 7, 72))
                .Ender()
                ;
        }

        private void LayerTabEv(jQueryEvent e)
        {
            MiPropPage.Ev(this, _content)
                .Ender()
                ;
        }

        #endregion Tabs
    }
}